package org.summerframework.core.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 石超
 * @version v1.0.0
 */
public class DateUtil extends DateUtils{

    public static final String DATETIME_SS_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMMSS_PATTERN = "yyyyMMddHHmmss";
    public static final String YYYYMMDD_PATTERN = "yyyyMMdd";
    public static final String YYMMDD_PATTERN = "yyMMdd";
    public static final String YYYY_PATTERN = "yyyy";

    public static Date addDate(Date date, int addDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, addDay);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    /**
     * Parse dateString
     *
     * @param dateString the date string
     * @return date the date
     * @author liaorb
     * @date 2019-05-29 13:48:56
     */
    public static Date parse(String dateString) {
        if(StringUtils.isBlank(dateString)){
            return null;
        }
        String pattern = DATE_FORMAT_PATTERN;
        if(dateString.length() == 19){
            pattern = DATETIME_FORMAT_PATTERN;
        }else if(dateString.length() >= 21){
            pattern = DATETIME_SS_FORMAT_PATTERN;
        }
        return parse(dateString,pattern);
    }

    /**
     * Parse dateString
     *
     * @param str     the str
     * @param pattern the pattern
     * @return date the date
     * @author liaorb
     * @date 2019-05-29 13:49:41
     */
    public static Date parse(String str, String pattern) {
        if (StringUtils.isBlank(str)) {
            return null;
        } else {
            try {
                return getDateFormat(pattern).parse(str);
            } catch (ParseException e) {
                throw new IllegalArgumentException("Can't parse " + str + " using " + pattern);
            }
        }
    }

    /**
     * 格式化
     *
     * @param date   the date
     * @param format the format
     * @return string the string
     * @author liaorb
     * @date 2019-06-26 10:41:10
     */
    public static String format(Date date,String format){
        if(null == date){
            return null;
        }
        return getDateFormat(format).format(date);
    }

    private static DateFormat getDateFormat(String format){
        return new SimpleDateFormat(format);
    }

    public static String getCurrentDateTimeString() {
        return getDateFormat(DATETIME_FORMAT_PATTERN).format(new Date());
    }

    public static String getCurrentDateTimeString(String format) {
        return getDateFormat(format).format(new Date());
    }
}
